package com.orangelife.service.linkBillType.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.linkBillType.IPropFeeLinkBillTypeService;
import com.orangelife.service.linkBillType.modal.PropFeeLinkBillTypeDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_13006015. All Rights Reserved.
 *****************************************************************/
/* ==============================================================
 * 功能名                            PropFeeLinkBillType类的 service层的增删改查    
 * @author          @author zhr
 * @version         1.1.0
 * @date             2016-07-04 13:54:37
 * ==============================================================*/
@Service("propFeeLinkBillTypeService")
public  class PropFeeLinkBillTypeServiceImpl extends MybaitsGenericDaoSubLogic implements IPropFeeLinkBillTypeService{	
private static Logger logger = Logger
			.getLogger(PropFeeLinkBillTypeServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeLinkBillTypeServiceImpl.logger = logger;
	}

    
	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class }) 		  
	 public int addPropFeeLinkBillType(PropFeeLinkBillTypeDto propFeeLinkBillType) throws AppBizException
 	{	
 	
 	 Assert.notNull(propFeeLinkBillType);
     /************************************************
	  * 判断输入参数的长度以及是否能为空
	  ************************************************/
 	       
       if (!StringUtils.isBlank(propFeeLinkBillType.getRuleType())){ 	
		 if ( propFeeLinkBillType.getRuleType().length()>10)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "计费类型长度超出" );	
		}	
		}	
	
if (!StringUtils.isBlank(propFeeLinkBillType.getRule())){ 	
		 if ( propFeeLinkBillType.getRule().length()>256)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "计费规则长度超出" );	
		}	
		}	
	
if (!StringUtils.isBlank(propFeeLinkBillType.getRuleParam())){ 	
		 if ( propFeeLinkBillType.getRuleParam().length()>256)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "计费参数长度超出" );	
		}	
		}	
	
if (!StringUtils.isBlank(propFeeLinkBillType.getRemark())){ 	
		 if ( propFeeLinkBillType.getRemark().length()>256)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出" );	
		}	
		}	
	

 	 
    	propFeeLinkBillType.setLinkID(IdUtil.getuuId());	
		propFeeLinkBillType.setItemStat(TypeConst.FLAG_ON);
		propFeeLinkBillType.setTscreate(new Date());
		propFeeLinkBillType.setTsedit(new Date());
		return this.getSqlSession().insert("PropFeeLinkBillTypeMapper.insertSelective",
				propFeeLinkBillType);
 	}
 
 
 
 
   
 
	 @Override
	 @Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	 public int editPropFeeLinkBillType(PropFeeLinkBillTypeDto propFeeLinkBillType) throws AppBizException
	 {
        
	  Assert.notNull(propFeeLinkBillType);
     /************************************************
	  * 判断输入参数的长度以及是否能为空
	  ************************************************/
	 if (StringUtils.isBlank(propFeeLinkBillType.getLinkID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
	 }
	 PropFeeLinkBillTypeDto oldPropFeeLinkBillType = this.findPropFeeLinkBillTypeByID(propFeeLinkBillType.getLinkID());
	
       
      if (!StringUtils.isBlank(propFeeLinkBillType.getRuleType())){ 	
		 if ( propFeeLinkBillType.getRuleType().length()>10)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "计费类型长度超出" );	
		}	
		}	
	
if (!StringUtils.isBlank(propFeeLinkBillType.getRule())){ 	
		 if ( propFeeLinkBillType.getRule().length()>256)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "计费规则长度超出" );	
		}	
		}	
	
if (!StringUtils.isBlank(propFeeLinkBillType.getRuleParam())){ 	
		 if ( propFeeLinkBillType.getRuleParam().length()>256)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "计费参数长度超出" );	
		}	
		}	
	
if (!StringUtils.isBlank(propFeeLinkBillType.getRemark())){ 	
		 if ( propFeeLinkBillType.getRemark().length()>256)	
		 {throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出" );	
		}	
		}	
	

	 if (oldPropFeeLinkBillType == null) {
			throw new AppBizException(CodeConst.BIS_CODE_13006, "实体不存在");
		}
		propFeeLinkBillType.setTsedit(new Date());
		return this.getSqlSession().update("PropFeeLinkBillTypeMapper.updateByPrimaryKeySelective",
				propFeeLinkBillType);
	}
 
 	 
 	 
 	 
	@Override
	public PropFeeLinkBillTypeDto findPropFeeLinkBillTypeByID(String propFeeLinkBillTypeID) throws AppBizException {
		if (StringUtils.isBlank( propFeeLinkBillTypeID)) {
		
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

         /************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeLinkBillTypeDto oldPropFeeLinkBillType = this.getSqlSession().selectOne("PropFeeLinkBillTypeMapper.selectByPrimaryKey", propFeeLinkBillTypeID);
		if (oldPropFeeLinkBillType == null) {
			throw new AppBizException(CodeConst.BIS_CODE_13006, "实体不存在");
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeLinkBillType.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_13006, "实体不存在");
		}
		return oldPropFeeLinkBillType;
 }
  @Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
 public int deletePropFeeLinkBillTypeByLogic(String propFeeLinkBillTypeID) throws AppBizException
 {
 
 if (StringUtils.isBlank(propFeeLinkBillTypeID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
        /************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeLinkBillTypeDto oldPropFeeLinkBillType = this.findPropFeeLinkBillTypeByID(propFeeLinkBillTypeID);

		oldPropFeeLinkBillType.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeLinkBillType.setTsedit(new Date());
		return this.getSqlSession().update("PropFeeLinkBillTypeMapper.updateByPrimaryKeySelective",
				oldPropFeeLinkBillType);
 } 	
 
 
 
 @Override
    public List<Map<String, Object>> findPropFeeLinkBillTypeByPage(String  commID, 
			String search, int pageNo, int pageSize) throws AppBizException
			{
		
		
		/************************************************
		 * 设置输入参数
		 ************************************************/	
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeLinkBillTypeMapper.getLinkBillType", map);
		return list;
			}
			
			
			

   @Override
    public List<PropFeeLinkBillTypeDto> getPropFeeLinkBillTypeByPage(String  tsCreate, 
			String search, int pageNo, int pageSize) throws AppBizException
			{
		/************************************************
		 * 设置输入参数
		 ************************************************/		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeLinkBillTypeDto> list = this.getSqlSession().selectList(
				"PropFeeLinkBillTypeMapper.getAllPropFeeLinkBillType", map);
		return list;
			}
			
			
			
			
	
    @Override
	public List<Map<String, Object>> selectPropFeeLinkBillTypeByRelevantID(String   linkID,	String   billTypeID,	String   feeTypeID,	String   ruleType	) throws AppBizException
	{
      /************************************************
		 * 设置输入参数
		 ************************************************/		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("linkID",linkID);	
		map.put("billTypeID",billTypeID);	
		map.put("feeTypeID",feeTypeID);	
		map.put("ruleType",ruleType);	
		
	     List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeLinkBillTypeMapper.selectPropFeeLinkBillTypeByRelevantID", map);
		return list;
	}


    @Override
	public List<PropFeeLinkBillTypeDto> findPropFeeLinkBillTypeByRelevantID(String   linkID,	String   billTypeID,	String   feeTypeID,	String   ruleType	) throws AppBizException
	{
      /************************************************
		 * 设置输入参数
		 ************************************************/		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("linkID",linkID);	
		map.put("billTypeID",billTypeID);	
		map.put("feeTypeID",feeTypeID);	
		map.put("ruleType",ruleType);	
		
	     List<PropFeeLinkBillTypeDto> list = this.getSqlSession().selectList(
				"PropFeeLinkBillTypeMapper.findPropFeeLinkBillTypeByRelevantID", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> findBillType(String commID, String search,
			int pageNo, int pageSize) throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeLinkBillTypeMapper.getBillType", map);
		return list;
		
	}

	@Override
	public List<Map<String, Object>> findFeeType(String commID, String search,
			int pageNo, int pageSize) throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeLinkBillTypeMapper.getFeeType", map);
		return list;
	}
}